/*
 * Logger.cpp
 *
 *  Created on: Oct 22, 2008
 *      Author: Ryan
 */

#include "Logger.h"

namespace OOInterface
{

Logger::LoggerMap Logger::_loggers = Logger::LoggerMap();

Logger::Logger( std::string filename )
{
	_filename = filename;
}

Logger::Logger( const Logger& l )
{
	_filename = l._filename;
	_stream = l._stream;
}

Logger::~Logger()
{
}

Logger& Logger::getInstance( const char* filename)
{
	return Logger::getInstance( std::string( filename ) );
}

Logger& Logger::getInstance( std::string filename )
{

	if( _loggers.find( filename ) != _loggers.end()  )
		return (*(_loggers.find( filename ))).second;


	Logger* logger = new Logger( filename );
	(*logger).open();
	_loggers.insert( std::pair< std::string, Logger >( filename, (*logger)));
	return (*logger);
}

bool Logger::open()
{
	_stream = new std::ofstream(_filename.c_str());

	return _stream->is_open();
}

void Logger::closeAll()
{
	for( LoggerMap::iterator it = _loggers.begin(); it != _loggers.end(); ++it)
	{
		((*it).second)._stream->close();
	}
}

Logger& Logger::operator<<( const char* rhs )
{
	*_stream << rhs;

	return *this;
}

Logger& Logger::operator<<( const Object& rhs )
{
	return operator<<( rhs.toString() );
}

Logger& Logger::operator<<( const std::string rhs )
{
	return operator<<( rhs.c_str() );
}

Logger& Logger::operator<<( bool rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( short rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( unsigned short rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( int rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( unsigned int rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( long rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( unsigned long rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( float rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( double rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( long double rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( void* rhs )
{
	*_stream << rhs;
	return *this;
}

Logger& Logger::operator<<( std::ostream& (*rhs) (std::ostream&) )
{
	*_stream << rhs;
	return *this;
}


}

